package com.example.myspringboot.reflex.getAttribute;

import com.example.myspringboot.reflex.TestClazz;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @Author ZhengShan
 * @Date 2024 06 26 16 51
 **/
public class TestConstructor {

    /**
     * Field:成员变量
     * Method:方法
     * Constructor:构造方法
     * Package:包
     * @param args
     */

    public static void main(String[] args) throws Exception{
        Class<TestClazz> clazz0 = TestClazz.class;

        TestClazz testClazz = new TestClazz();
        Class clazz1 = testClazz.getClass();

        try {
            @SuppressWarnings("unchecked")
            Class<TestClazz> clazz2 = (Class<TestClazz>)Class.forName("com.example.myspringboot.reflex.TestClazz");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        /**
         * getDeclaredFields() 只可以访问本类当中的属性
         * getFields()只能访问声明为public的属性
         * getDeclaredFields() 可以访问声明为任意权限修饰符的属性
         * getFields() 可以访问父类中的声明为public的属性
         * 获取属性
         */
        //获取public的属性
        Field[] fields = clazz0.getFields();
        for(Field f : fields){
            System.out.println("======:" + f);
        }
        //获取全部定义的属性
        Field[] fieldsAll = clazz0.getDeclaredFields();
        for(Field f : fieldsAll){
            System.out.println("-----:" + f);
            //获取权限修饰符
            // 权限修饰符为default       返回值0
            //权限修饰符为public         返回值1
            //权限修饰符为private       返回值2
            //权限修饰符为protected   返回值4
            System.out.println("+++++++:" + f.getModifiers());
            //Modifier.toString(int i) 以字符串形式打印出来
            System.out.println("```````:" + Modifier.toString(f.getModifiers()));
            //获取变量类型
            System.out.println("*******:" + f.getType().getName());
            //获取变量名
            System.out.println("^^^^^^^:" + f.getName());
        }

        /**
         * 获取方法
         * Method[] methods =  clazz.getMethods();
         * Method[] methods2 =  clazz.getDeclaredMethods();
         */
        System.out.println("-------------------------------------------------------");
        TestClazz testClazz1 = new TestClazz();
        Method method = clazz1.getMethod("setTestPrivate", String.class);
        Method methodGet = clazz1.getMethod("getTestPrivate");
        method.invoke(testClazz1, "test");
        System.out.println(methodGet.invoke(testClazz1));

        //获取全部方法
        System.out.println("----------------------获取全部方法-------------------------");
        Method[] methods =  clazz1.getMethods();
        Method[] methods2 =  clazz1.getDeclaredMethods();
        for(Method m1 : methods){
            System.out.println("=============" + m1);
        }
        System.out.println("-----------------------------------------------------------");
        for(Method m2 : methods2){
            System.out.println("=============" + m2);
        }

        /**
         * 获取注解
         */
        System.out.println("-------------------------获取注解-----------------------------");
        //获取类注解
        Annotation[] annotationsClass = clazz1.getAnnotations();
        for(Annotation a : annotationsClass) {
            System.out.println("annotationsClass========+++" + a);
        }
        for(Method m3 : methods2){
            Annotation[] annotations = m3.getAnnotations();
            for(Annotation a : annotations) {
                System.out.println("aaaaaaaaaaa+++" + a);
            }
            System.out.println("getName:" + m3.getName());
        }

    }

}
